#多号换行，变量名：sfsyUrl
# const $ = new Env('顺丰速运')
import hashlib
import json
import os
import random
import time
from datetime import datetime, timedelta
from sys import exit
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

IS_DEV = False
if os.path.isfile('DEV_ENV.py'):
    import DEV_ENV

        IS_DEV = True


        #from notify import send

        send_msg = ''
        one_msg = ''


        def Log(cont=''):
            global send_msg, one_msg
                print(cont)
                    if cont:
                            one_msg += f'{cont}\n'
                                    send_msg += f'{cont}\n'


                                    inviteId = ['']


                                    class RUN:
                                        def __init__(self, info, index):
                                                global one_msg
                                                        one_msg = ''
                                                                split_info = info.split('@')
                                                                        url = split_info[0]
                                                                                len_split_info = len(split_info)
                                                                                        last_info = split_info[len_split_info - 1]
                                                                                                self.send_UID = None
                                                                                                        if len_split_info > 0 and "UID_" in last_info:
                                                                                                                    self.send_UID = last_info
                                                                                                                            self.index = index + 1
                                                                                                                                    Log(f"\n---------开始执行第{self.index}个账号>>>>>")
                                                                                                                                            self.s = requests.session()
                                                                                                                                                    self.s.verify = False
                                                                                                                                                            self.headers = {
                                                                                                                                                                            'Host': 'mcs-mimp-web.sf-express.com',
                                                                                                                                                                                        'upgrade-insecure-requests': '1',
                                                                                                                                                                                                    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090551) XWEB/6945 Flue',
                                                                                                                                                                                                                'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                                                                                                                                                                                                                            'sec-fetch-site': 'none',
                                                                                                                                                                                                                                        'sec-fetch-mode': 'navigate',
                                                                                                                                                                                                                                                    'sec-fetch-user': '?1',
                                                                                                                                                                                                                                                                'sec-fetch-dest': 'document',
                                                                                                                                                                                                                                                                            'accept-language': 'zh-CN,zh',
                                                                                                                                                                                                                                                                                        'platform': 'MINI_PROGRAM',

                                                                                                                                                            }
                                                                                                                                                                    self.anniversary_black = False
                                                                                                                                                                            self.member_day_black = False
                                                                                                                                                                                    self.member_day_red_packet_drew_today = False
                                                                                                                                                                                            self.member_day_red_packet_map = {}
                                                                                                                                                                                                    self.login_res = self.login(url)
                                                                                                                                                                                                            self.today = datetime.now().strftime('%Y-%m-%d')
                                                                                                                                                                                                                    # self.answer = APP_INFO.get('ANSWER', []).get(self.today, False)
                                                                                                                                                                                                                            self.max_level = 8
                                                                                                                                                                                                                                    self.packet_threshold = 1 << (self.max_level - 1)

                                                                                                                                                                                                                                        def get_deviceId(self, characters='abcdef0123456789'):
                                                                                                                                                                                                                                                result = ''
                                                                                                                                                                                                                                                        for char in 'xxxxxxxx-xxxx-xxxx':
                                                                                                                                                                                                                                                                    if char == 'x':
                                                                                                                                                                                                                                                                                    result += random.choice(characters)
                                                                                                                                                                                                                                                                                                elif char == 'X':
                                                                                                                                                                                                                                                                                                                result += random.choice(characters).upper()
                                                                                                                                                                                                                                                                                                                            else:
                                                                                                                                                                                                                                                                                                                                            result += char
                                                                                                                                                                                                                                                                                                                                                    return result

                                                                                                                                                                                                                                                                                                                                                        def login(self, sfsyUrl):
                                                                                                                                                                                                                                                                                                                                                                ress = self.s.get(sfsyUrl, headers=self.headers)
                                                                                                                                                                                                                                                                                                                                                                        # print(ress.text)
                                                                                                                                                                                                                                                                                                                                                                                self.user_id = self.s.cookies.get_dict().get('_login_user_id_', '')
                                                                                                                                                                                                                                                                                                                                                                                        self.phone = self.s.cookies.get_dict().get('_login_mobile_', '')
                                                                                                                                                                                                                                                                                                                                                                                                self.mobile = self.phone[:3] + "*" * 4 + self.phone[7:]
                                                                                                                                                                                                                                                                                                                                                                                                        if self.phone != '':
                                                                                                                                                                                                                                                                                                                                                                                                                    Log(f'用户:【{self.mobile}】登陆成功')
                                                                                                                                                                                                                                                                                                                                                                                                                                return True
                                                                                                                                                                                                                                                                                                                                                                                                                                        else:
                                                                                                                                                                                                                                                                                                                                                                                                                                                    Log(f'获取用户信息失败')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                return False

                                                                                                                                                                                                                                                                                                                                                                                                                                                                    def getSign(self):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            timestamp = str(int(round(time.time() * 1000)))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    token = 'wwesldfs29aniversaryvdld29'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            sysCode = 'MCS-MIMP-CORE'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    data = f'token={token}&timestamp={timestamp}&sysCode={sysCode}'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            signature = hashlib.md5(data.encode()).hexdigest()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    data = {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    'sysCode': sysCode,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                'timestamp': timestamp,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'signature': signature
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            self.headers.update(data)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return data

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        def do_request(self, url, data={}, req_type='post'):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                self.getSign()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        try:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if req_type.lower() == 'get':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    response = self.s.get(url, headers=self.headers)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                elif req_type.lower() == 'post':
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                response = self.s.post(url, headers=self.headers, json=data)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            else:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            raise ValueError('Invalid req_type: %s' % req_type)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        res = response.json()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return res
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            except requests.exceptions.RequestException as e:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        print('Request failed:', e)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return None
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            except json.JSONDecodeError as e:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        print('JSON decoding failed:', e)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return None

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        def sign(self):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                print(f'>>>>>>开始执行签到')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        json_data = {"comeFrom": "vioin", "channelFrom": "WEIXIN"}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                url = 'https://mcs-mimp-web.sf-express.com/mcs-mimp/commonPost/~memberNonactivity~integralTaskSignPlusService~automaticSignFetchPackage'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        response = self.do_request(url, data=json_data)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                # print(response)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if response.get('success') == True:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    count_day = response.get('obj', {}).get('countDay', 0)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if response.get('obj') and response['obj'].get('integralTaskSignPackageVOList'):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                packet_name = response["obj"]["integra"]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                            }